﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UCS.XNA.Utils;

namespace Hopeless.Metaphysical.GUI
{
    class MainMenuWindow
    {
        public MainMenuWindow(HopelessGame game)
        {
            _game = game;
        }

        private HopelessGame _game;
        private SpriteFont _menuFont;
        private SpriteFont _titleFont;
        private Menu _menu;
        private Rectangle _space;
        private Vector2 _titlePosition;

        private void InitializeMenu()
        {
            var widht = _space.Width * 0.3f;
            var height = _space.Height * 0.15f;
            var x = _space.X + _space.Width / 2f - widht / 2f;
            var y = _space.Y + _space.Height * 0.5f;
            var menuSpace = new Rectangle((int)x, (int)y, (int)widht, (int)height);

            var whiteTexture = Texture2DCreator.FromColor(Color.White, GameResources.Graphics.GraphicsDevice);
            var transparentTexture = Texture2DCreator.FromColor(Color.Transparent, GameResources.Graphics.GraphicsDevice);

            _menuFont = GameResources.Content.Load<SpriteFont>("Trebuchet MS");
            _menu = new Menu(menuSpace);
            var item1 = new MenuItem("Start", _menuFont);
            item1.ForeColor = Color.White;
            item1.HoveringForeColor = Color.Black;
            item1.Border = new Border(whiteTexture, whiteTexture);
            item1.HoveringBorder = new Border(whiteTexture, whiteTexture);
            item1.BackgroundTexture = transparentTexture;
            item1.BackgroundHoveringTexture = whiteTexture;
            item1.Click += (sender, e) => _game.Start();

            var item2 = new MenuItem("Exit", _menuFont);
            item2.ForeColor = Color.White;
            item2.HoveringForeColor = Color.Black;
            item2.Border = new Border(whiteTexture, whiteTexture);
            item2.HoveringBorder = new Border(whiteTexture, whiteTexture);
            item2.BackgroundTexture = transparentTexture;
            item2.BackgroundHoveringTexture = whiteTexture;
            item2.Click += (sender, e) => _game.Exit();

            _menu.AddItem(item1);
            _menu.AddItem(item2);
        }

        public void Initialize(Rectangle space)
        {
            _space = space;

            _titleFont = GameResources.Content.Load<SpriteFont>("Calibri Big");
            var size = _titleFont.MeasureString(HopelessGame.TITLE);
            var x = _space.X + _space.Width / 2 - size.X / 2;
            var y = _space.Y + _space.Height * 0.2f;
            _titlePosition = new Vector2(x, y);

            this.InitializeMenu();
        }

        public void Update(GameTime gameTime)
        {
            _menu.Update(gameTime, Mouse.GetState(), Keyboard.GetState());
        }

        public void Draw(GameTime gameTime)
        {
            _menu.Draw(gameTime);

            GameResources.SpriteBatch.Begin();

            GameResources.SpriteBatch.DrawString(_titleFont, HopelessGame.TITLE, _titlePosition, Color.White);

            GameResources.SpriteBatch.End();
        }
    }
}
